<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/24/024
 * Time: 17:15
 */

namespace backend\models\forms;


use yii\db\ActiveRecord;

class DealForm extends ActiveRecord
{
    public $deal_up;
    public $deal_down;
    public $num = 10;
    public $total_diamond;
    public $player_diamond;

    public function rules()
    {
        return [
            [['deal_down','deal_up'],'validateCompare'],
            [['deal_up','deal_down'],'integer','min' => 0],
            [['deal_down','deal_up'],'validateMod'],
            ['deal_up','validateUp'],
            ['deal_down','validateDown'],
            [['total_diamond','player_diamond'],'safe'],
            [['deal_up','deal_down'],'validateRequired'],
        ];
    }

    public function beforeValidate()
    {
        $this->deal_down = $this->deal_down*1;
        $this->deal_up = $this->deal_up*1;
        return parent::beforeValidate(); // TODO: Change the autogenerated stub
    }

    /**
     * 验证不能为空
     * @param $attribute
     */
    public function validateRequired($attribute)
    {
        if($this->deal_up*1 == 0 && $this->deal_down*1 == 0){
            $this->addError($attribute, '不能同时为空');
        }
    }

    /**
     * 验证不能同时开洗分
     * @param $attribute
     */
    public function validateCompare($attribute)
    {
        if ($this->deal_up>0 && $this->deal_down>0) {
            $this->addError($attribute, '不能同时开洗分');
        }
    }

    /**
     * 验证能不能被100整除
     * @param $attribute
     */
    public function validateMod($attribute)
    {
        if($this->deal_down%$this->num != 0 || $this->deal_up%$this->num != 0){
            $this->addError($attribute, "必须是{$this->num}的倍数");
        }
    }

    /**
     * 验证代理商钻石够不够
     * @param $attribute
     */
    public function validateUp($attribute)
    {
        if(($this->total_diamond - $this->deal_up) < 0){
            $this->addError($attribute, "代理商钻石不足");
        }
    }

    /**
     * 验证玩家钻石够不够
     * @param $attribute
     */
    public function validateDown($attribute)
    {
        if(($this->player_diamond-$this->deal_down) < 0){
            $this->addError($attribute, "玩家钻石不足");
        }
    }


    public function getPointAndDealType()
    {
        $arr = [
            'type' => null,
            'point' => 0,
        ];

        if(!empty($this->deal_down)){
            $arr['point'] = $this->deal_down;
            $arr['type'] = 0;
        }else{
            $arr['point'] = $this->deal_up;
            $arr['type'] = 1;
        }
        return $arr;
    }

}